import datetime,time

class stime(datetime.time):
    def __sub__(self,sub):
        return (self.hour-sub.hour)	*3600+\
            (self.minute-sub.minute)	*60+\
            (self.second-sub.second)	*1
voidfunc=lambda *args,**kwargs:None

class TimePeriod:
    def __init__(self,start,stop,enterAction=voidfunc,exitAction=voidfunc):
        self.start=start
        self.stop=stop
        self.enterAction=enterAction
        self.exitAction=exitAction
    def __contains__(self,time):
        return self.start<=time<=self.stop
    def __lt__(self,time):
        '''Define behavior when: TimePeriod<stime'''
        return time>self.stop
    def __gt__(self,time):
        '''Define behavior when: TimePeriod>stime'''
        return self.start>time
    def __sub__(self,time):
        '''Return seconds till this TimePeriod starts'''
        return self.start-time
    def setAction(self,enterAction):
        self.enterAction=enterAction
        return self

class TimeConstructor:
    def __getitem__(self,key):
        '''
        Construct TimePeriod conveniently

        Usage:
        t=TimeConstructor()
        a=t[10:30,11:30]
        '''
        start,stop=key 
        startTime=stime(start.start,start.stop)
        stopTime=stime(stop.start,stop.stop)
        return TimePeriod(startTime,stopTime)

def runtask(task,tillExit):
    try:
        result=task.enterAction(tillExit)
    except BaseException:
        return
       
def run(tasks):
    for task in tasks:
        now=datetime.datetime.today().time()
        if task>now:
            time.sleep(task-now)
            runtask(task,tillExit=task.stop-task.start)
        elif now in task:
            runtask(task,tillExit=task.stop-now)
if __name__=='__main__':
    a=stime(1,10)
    b=stime(2,11)
    print(b-a)
